<?php

namespace App\Orchid\Screens;

use App\Models\Application;
use Orchid\Screen\Screen;
use Orchid\Screen\Fields\Input;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\Actions\ModalToggle;
use Illuminate\Http\Request;
use Orchid\Screen\TD;
use Orchid\Screen\Actions\Button;

class ApplicationScreen extends Screen
{
    /**
     * Fetch data to be displayed on the screen.
     *
     * @return array
     */
    public function query(): iterable
    {
        return [
            'applications' => Application::latest()->get(),
        ];
    }

    /**
     * The name of the screen displayed in the header.
     *
     * @return string|null
     */
    public function name(): ?string
    {
        return 'Обращения';
    }

    /**
     * The screen's action buttons.
     *
     * @return \Orchid\Screen\Action[]
     */
    public function commandBar(): iterable
    {
        return [
            ModalToggle::make('Добавить')
                ->modal('taskModal')
                ->method('create')
                ->icon('plus'),
        ];
    }

    /**
     * The screen's layout elements.
     *
     * @return \Orchid\Screen\Layout[]|string[]
     */
    public function layout(): iterable
    {
        return [
            Layout::table('applications', [
                TD::make('fio', 'ФИО'),
                TD::make('email', 'Email'),
                TD::make('phone', 'Телефон'),
                TD::make('text', 'Текст'),
                TD::make('Действия')
                    ->alignRight()
                    ->render(function (Application $application) {
                        return Button::make('Удалить')
                            ->confirm('Обращение будет удалено')
                            ->method('delete', ['application' => $application->id]);
                    }),
            ]),
            Layout::modal('taskModal', Layout::rows([
                Input::make('application.fio')
                    ->title('ФИО'),
                Input::make('application.email')
                    ->title('Email'),
                Input::make('application.phone')
                    ->title('Телефон'),
                Input::make('application.text')
                    ->title('Текст'),
            ]))
                ->title('Создать обращение')
                ->applyButton('Добавить обращение'),
        ];
    }

    public function create(Request $request)
    {
        $request->validate([
            'application.fio' => 'required|max:255',
            'application.email' => 'required|max:255',
            'application.phone' => 'required|max:255',
            'application.text' => 'required|max:255',
        ]);

        $application = new Application();
        $application->fio = $request->input('application.fio');
        $application->email = $request->input('application.email');
        $application->phone = $request->input('application.phone');
        $application->text = $request->input('application.text');
        $application->save();
    }

    public function delete(Application $application)
    {
        $application->delete();
    }
}
